<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h2>欢迎来到巧牛运营平台</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
import useUserStore from '@/store/modules/user'

//获取路由对象
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
let loginForms = ref()

let loading = ref(false)
//收集账号密码数据
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
//登录按钮回调
const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    ElNotification({
      title: `${getTime()}`,
      message: '欢迎登录巧牛运营平台',
      type: 'success',
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      title: '登录失败',
      message: (error as Error).message,
      type: 'error',
    })
  }
}
//@ts-ignore
const validatorUsername = (rule: any, value: any, callback: any) => {
  if (value.length > 4 && value.length < 8) {
    callback()
  } else {
    callback(new Error('账号格式不正确'))
  }
}
//@ts-ignore
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (/^\d{6,15}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码格式不正确至少6位数字'))
  }
}
//表单验证配置对象
const rules = {
  username: [{ trigger: 'change', validator: validatorUsername }],
  password: [{ trigger: 'change', validator: validatorPassword }],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background1.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 60vh;
    top: 30vh;
    right: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
